package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatOtherSystemSettingType;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.role.MmochatTempNpc;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_1_21S extends SkymobiProtocolMessage {
	MmochatPlayer me = null;
	int line = 0;
	MmochatMap map = null;
	Map<Integer, MmochatPlayer> sceenRoles = new ConcurrentHashMap<Integer, MmochatPlayer>();

	public Mmochat_1_21S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_1_21);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		if (me != null) {
			out.putInt(map.getMapId());
			out.put((byte) line);
			if (map.isHasWeather()) {
				out.put((byte) map.getCurWeather().getValue());
			} else {
				out.put((byte) 0);
			}

			// put others
			int roleNum = sceenRoles.size();
			out.putShort((short) roleNum);
			for (MmochatPlayer role : sceenRoles.values()) {
				out.putInt(role.getRoleId());
				out.putShort((short) role.getX());
				out.putShort((short) role.getY());
				out.put((byte) role.getRoleType().getValue());
				out.putUnsignedInt(role.getStateValue(me));
				out.putPrefixedString(role.getName(), 2);

				if (me
						.getOtherSystemSettingByType(MmochatOtherSystemSettingType.不显示他人等级)) {
					if (role.getCurChenHao() == null) {
						out.put((byte) 0);
					} else {
						out.putPrefixedString(role.getCurChenHaoInfo(), 1);
					}
				} else {
					// 显示他人等级
					out.putPrefixedString(role.getLevelInfo(), 1);
				}

			}

			// npc
			List<MmochatNpc> npcs = null;
			List<MmochatNpc> needUpdateFixedNpc = new ArrayList<MmochatNpc>();
			List<MmochatNpc> tmpNpc = new ArrayList<MmochatNpc>();
			if (map.getLineNpcMap(line) != null) {
				npcs = new ArrayList<MmochatNpc>(map.getLineNpcMap(line)
						.values());
				for (MmochatNpc npc : npcs) {
					me.addTmpNpc(npc.getNpcId());
					if (npc instanceof MmochatTempNpc) {
						tmpNpc.add(npc);
					} else {
						if (npc.getTaskStateValue(me) != 0) {
							needUpdateFixedNpc.add(npc);
						}
					}
				}
			}
			// put fixed npc taskState
			out.putShort((short) needUpdateFixedNpc.size());
			for (MmochatNpc npc : needUpdateFixedNpc) {
				out.putInt(npc.getNpcId());
				out.put((byte) npc.getTaskStateValue(me));
			}

			// put tmp npc
			out.putShort((short) tmpNpc.size());
			for (MmochatNpc npc : tmpNpc) {
				out.putInt(npc.getNpcId());
				if (npc instanceof MmochatTempNpc) {
					out.put((byte) 1);
				} else {
					out.put((byte) 0);
				}
				out.put((byte) npc.getTaskStateValue(me));
				out.putShort(npc.getX());
				out.putShort(npc.getY());
				out.putUnsignedShort(npc.getBmpType().getValue());
				out.put((byte) npc.getFace().getValue());
				out.putPrefixedString(npc.getName(), 1);
			}
		}
	}

	public void setMe(MmochatPlayer me) {
		this.me = me;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public void setMap(MmochatMap map) {
		this.map = map;
	}

	public void setSceenRoles(Map<Integer, MmochatPlayer> sceenRoles) {
		this.sceenRoles = sceenRoles;
	}

	public void addSceenRole(MmochatPlayer role) {
		sceenRoles.put(role.getRoleId(), role);
	}
}
